<?php

abstract class Core_Data_GenericModel {

    protected static $objInstance;

    private $Connection;
    private $arrRecords = array ();
    private $strTableName;

    function __construct ( $Connection, $strTableName ) {
        $this->Connection = $Connection;
        $this->strTableName = $strTableName;
    }

    /**
     * Inserts a record into the table.
     *
     * @param object $objRecord
     * @return integer The insert key, if any.
     */
    public function insert ( $objRecord ) {

        $strRecordClassName = "Model_" . $this->getTableName() . "_Record";
        if ( $objRecord instanceof $strRecordClassName ) {

            // Create the query.
            $strQuery = "INSERT INTO `" . $this->getTableName() . "` SET ";
            foreach ( $objRecord->getUpdateValues() as $strFieldName => $mixValue ) {
                $strQuery .= "`" . $strFieldName . "` = ";
                if ( is_integer( $mixValue ) ) {
                    $strQuery .= $mixValue;
                } else {
                    $strQuery .= "`" . $this->Connection->escape( $mixValue ) . "`";
                }
                $strQuery .= ", ";
            }
            $strQuery = substr( $strQuery, 0, -2 );

            // Send the query.
            $this->Connection->query( $strQuery );

            // Return the primary key for this record, if any.
            if ( $objRecord->getPrimaryKey() ) {
                return $this->Connection->getInsertKey();
            }

        } else {
            die( "Only insertion of single records are supported at this time." );
        }

    }

    private function getTableName () {
        return $strTableName;
    }

    public function update () {

        $mixArg1 = func_get_arg(0);

        $strRecordClassName = "Model_" . $this->getTableName() . "_Record";
        if ( $mixArg1 instanceof $strRecordClassName ) {
            // This is an update of a single record.

            // We've now established that this is a record, so lets rename it accordingly.
            $objRecord = $mixArg1;

            // Create the query.
            $strQuery = "UPDATE `" . $this->getTableName() . "` SET ";
            foreach ( $objRecord->getUpdateValues() as $strFieldName => $mixValue ) {
                $strQuery .= "`" . $strFieldName . "` = ";
                if ( is_integer( $mixValue ) ) {
                    $strQuery .= $mixValue;
                } else {
                    $strQuery .= "`" . $this->Connection->escape( $mixValue ) . "`";
                }
                $strQuery .= ", ";
            }
            $strQuery = substr( $strQuery, 0, -2 );
            $strQuery .= " WHERE `" . $objRecord->getPrimaryKey() . "` = " . $objRecord->getPrimaryKeyValue();

            // Send the query.
            $this->Connection->query( $strQuery );

        } else {
            // This is some other kind of update.

            die( "Only updates of single records are supported at this time." );
        }

    }





    public function create () {

    }

    public function find ( array $arrMatches ) {

    }

    public function fintOne () {
        
    }

}

?>
